package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.seckill.SeckillFeignClient;
import com.atguigu.gmall.seckill.entity.SeckillGoods;
import com.atguigu.gmall.seckill.vo.SeckillOrderConfirmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    SeckillFeignClient seckillFeignClient;

    //跳转至秒杀页面
    @GetMapping("/seckill.html")
    public String seckillPage(Model model){
        //远程调用 秒杀服务，获取当天参与秒杀的所有商品
        Result<List<SeckillGoods>> seckillGoods = seckillFeignClient.getTodaySeckillGoods();
        model.addAttribute("list", seckillGoods.getData());
        return "seckill/index";
    }

    //商品详情
    @GetMapping("/seckill/{skuId}.html")
    public String seckillDetail (Model model,@PathVariable("skuId") Long skuId){
        //远程调用
        Result<SeckillGoods> seckillGoodsDetail = seckillFeignClient.getSeckillGoodsDetail(skuId);
        model.addAttribute("item", seckillGoodsDetail.getData());
        return "seckill/item";
    }

    //跳转秒杀排队页面
    @GetMapping("/seckill/queue.html")
    public String queuePage(@RequestParam("skuId") Long skuId,
                            @RequestParam("skuIdStr") String skuIdStr,
                            Model model){
        model.addAttribute("skuId", skuId);
        model.addAttribute("skuIdStr", skuIdStr);
        return "seckill/queue";
    }

    //秒杀订单确认页
    @GetMapping("/seckill/trade.html")
    public String tardePage(Model model,@RequestParam("code") String code){
        //远程调用秒杀服务 获取订单确认页数据
        SeckillOrderConfirmVo data = seckillFeignClient.getSeckillOrderInfo(code).getData();
        //页面数据：商品清单
        model.addAttribute("detailArrayList", data.getDetailArrayList());
        model.addAttribute("totalNum", data.getTotalNum());
        model.addAttribute("totalAmount", data.getTotalAmount());
        model.addAttribute("userAddressList", data.getUserAddressList());
        return "seckill/trade";
    }
}
